
package com.funo.government.info.view;

import java.util.ArrayList;

import javax.crypto.spec.IvParameterSpec;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.funo.government.info.R;
import com.funo.government.info.bean.AreaInfo;
import com.funo.government.info.constant.Constant;
import com.funo.government.info.utils.BusinessException;
import com.funo.government.info.utils.BusinessResolver.BusinessCallback;
import com.funo.government.info.utils.GovernmentResolver;
import com.funo.government.info.utils.PreferenceUtils;
import com.funo.government.info.utils.RequestTask;

public class AreaSelectActivity extends BaseActivity {

    private ListView lvArea;

    private SharedPreferences preferences;

    private LayoutInflater mInflater;

    private String areaId;

    private String areaName;

    private RequestTask mAreaListTask;

    private ProgressBar mProgressBar;

    public static final int REQUEST_CODE = 333;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_select);
        lvArea = (ListView) findViewById(R.id.lvArea);
        mProgressBar = (ProgressBar) findViewById(R.id.progress);
        mInflater = getLayoutInflater();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        areaId = preferences.getString(Constant.PRE_KEY_AREA_ID,
                getString(R.string.default_area_id));
        areaName = preferences.getString(Constant.PRE_KEY_AREA_NAME,
                getString(R.string.default_area_name));
        setResult(RESULT_CANCELED);
        lvArea.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
                AreaListAdapter adapter = (AreaListAdapter) parent.getAdapter();

                AreaInfo areaInfo = adapter.getItem(position);
                if (areaId.equals(areaInfo.areaId)) {
                    finish();
                    return;
                }
                preferences.edit().putString(Constant.PRE_KEY_AREA_ID, areaInfo.areaId)
                        .putString(Constant.PRE_KEY_AREA_NAME, areaInfo.areaName).commit();
                Intent intent = new Intent();
                intent.putExtra(Constant.PRE_KEY_AREA_ID, areaInfo.areaId);
                intent.putExtra(Constant.PRE_KEY_AREA_NAME, areaInfo.areaName);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        // ArrayList<AreaInfo> areaInfos = new ArrayList<AreaInfo>();
        // AreaInfo a1 = new AreaInfo("连城县", "1");
        // areaInfos.add(a1);
        // AreaInfo a2 = new AreaInfo("上杭县", "2");
        // areaInfos.add(a2);
        // AreaInfo a3 = new AreaInfo("武平县", "3");
        // areaInfos.add(a3);
        // AreaInfo a4 = new AreaInfo("新罗区", "4");
        // areaInfos.add(a4);
        // AreaInfo a5 = new AreaInfo("永定县", "5");
        // areaInfos.add(a5);
        // AreaInfo a6 = new AreaInfo("漳平市", "6");
        // areaInfos.add(a6);
        // AreaInfo a7 = new AreaInfo("长汀县", "7");
        // areaInfos.add(a7);
        // lvArea.setAdapter(new AreaListAdapter(areaInfos));
        mAreaListTask = GovernmentResolver.areaList(new BusinessCallback<ArrayList<AreaInfo>>() {

            @Override
            public void onSuccess(ArrayList<AreaInfo> areaInfos) {
                if (areaInfos != null && areaInfos.size() > 0) {
                    lvArea.setAdapter(new AreaListAdapter(areaInfos));

                } else {
                    AreaInfo areaInfo = new AreaInfo(areaName, areaId);
                    ArrayList<AreaInfo> list = new ArrayList<AreaInfo>();
                    list.add(areaInfo);
                    lvArea.setAdapter(new AreaListAdapter(list));
                }
                mProgressBar.setVisibility(View.GONE);
            }

            @Override
            public void onError(BusinessException e) {
                e.printStackTrace();
                AreaInfo areaInfo = new AreaInfo(areaName, areaId);
                ArrayList<AreaInfo> list = new ArrayList<AreaInfo>();
                list.add(areaInfo);
                lvArea.setAdapter(new AreaListAdapter(list));
                mProgressBar.setVisibility(View.GONE);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeTask(mAreaListTask);
    }

    private void closeTask(RequestTask task) {
        if (task != null) {
            task.cancel(true);
            task = null;
        }
    }

    private class AreaListAdapter extends BaseAdapter {

        public AreaListAdapter(ArrayList<AreaInfo> areaInfos) {
            this.areaInfos = areaInfos;
        }

        private ArrayList<AreaInfo> areaInfos;

        @Override
        public int getCount() {
            if (areaInfos == null)
                return 0;
            return areaInfos.size();
        }

        @Override
        public AreaInfo getItem(int position) {
            return areaInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_area_list, null);
                vh = new ViewHolder();
                vh.tvAreaName = (TextView) convertView.findViewById(R.id.tvAreaName);
                vh.ivCheck = (ImageView) convertView.findViewById(R.id.ivCheck);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            AreaInfo areaInfo = getItem(position);
            vh.tvAreaName.setText(areaInfo.areaName);
            if (areaId.equals(areaInfo.areaId)) {
                vh.ivCheck.setVisibility(View.VISIBLE);
            } else {
                vh.ivCheck.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

    }

    private class ViewHolder {
        TextView tvAreaName;

        ImageView ivCheck;

    }
}
